require 'map_editor/map_panel'
require 'map_editor/io'
require 'event_editor/map_move_controler'
module EventEditor
  module Commands
    class SetEventPosition
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("Map Move",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @map_panel = nil
        @model = model
        box = Gtk::HBox.new

        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all
     
        @dialog.set_size_request(600, 600)
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.create_panel
        @map_container = Gtk::VBox.new
        @event_combobox = ::Gadgets::Combobox.new(@model.event_select_list, [String, Integer])
        @map_container.pack_start(@event_combobox, false, false)
        return @map_container
      end
      
      def self.load_map(map_model, event_model, sx = 0, sy = 0)
        @map_model = map_model
        @map_container.remove(@map_panel) if @map_panel
        @controler = MapMoveControler.new(map_model, sx, sy)
        @map_panel = MapEditor::MapPanel.new(map_model, 2, {:map_move => @controler}, :map_move, 0, false)
        @map_container.add(@map_panel)
        @dialog.vbox.show_all
        GC.start
      end
      
      def self.show_all
      
      end
      
      def self.load(edit_command)
        if edit_command.empty?
          map_model = MapEditor::IO.load(@model.map_model.map_id)
          @event_combobox.select(edit_command[1])
          self.load_map(map_model, nil)
        else
          
          map_model = MapEditor::IO.load(edit_command[1])
          self.load_map(map_model, nil, edit_command[2], edit_command[3])
        end
      end
      
      def self.command
        return ['set_event_position', @event_combobox.selected_data, @controler.sx, @controler.sy]
      end
      
      def self.command_to_s(model, args, margin = "")
        name = '[<span color="red">invalid</span>]'
        model.event_select_list.each do |t|
          if t[1] == args[1]
            name = "[ " + t[0] + " ]"
            break
          end
        end
          
        s = ""
        s += "Set Event Position"
        s +=  "(#{name} : #{args[2]}, #{args[3]})"
        return s
      end
      
      def self.icon
        return @@icon
      end
    end
  end
end